package cn.yunhe.controller;

import cn.yunhe.model.Employee;
import cn.yunhe.service.EmployeeService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created by Administrator on 2017/7/22.
 */
@Controller
@RequestMapping("/employee")
public class EmployeeController {

    private Logger logger = (Logger) LogManager.getLogger(EmployeeController.class);

    @Autowired
    private EmployeeService employeeService;

    @RequestMapping("/searchAll")
    public ModelAndView searchAll(){
        List<Employee> list = employeeService.searchAll();
        logger.info(list.size());
        /*Map<String,Object> map=new HashMap<String, Object>();
        map.put("employeeList",list);
        ModelAndView mv=new ModelAndView("main",map);*/
        ModelAndView mv = new ModelAndView("main");
        mv.addObject("employeeList",list);
        return mv;
    }

    @RequestMapping("/findEmployee")
    public ModelAndView findEmployee(Integer id){
        Employee employee = employeeService.findEmployee(id);
        if (employee != null){
            ModelAndView mv = new ModelAndView("update");
            mv.addObject("employee",employee);
            return mv;
        }else {
            return new ModelAndView("main");
        }
    }

    @RequestMapping("/updateEmployee")
    public ModelAndView updateEmployee(Employee employee){
        logger.info(employee);
        employeeService.updateEmployee(employee);
        return new ModelAndView("redirect:/employee/searchAll");
    }

    @RequestMapping("/deleteEmployee")
    public ModelAndView deleteEmployee(Integer id){
        employeeService.deleteEmployee(id);
        return new ModelAndView("redirect:/employee/searchAll");
    }
}
